<?php
namespace Admin\Controller;

use Think\Controller;

class PublicOperateController extends Controller
{

    public function changePwd()
    {
        sess();
        $password = I('post.password');
        if (I('post.password') != I('post.repassword')) {
            $this->error('两次输入密码不一致！');
        }
        $oldpassword = md5(md5((I('post.oldpassword'))));
        $id = session('uid');
        $User = M("User");
        if ($User->getFieldById($id, 'password') != $oldpassword) {
            $this->error('旧密码错误');
        } else {
            if (empty($password) || strlen($password) < 5) {
                $this->error('密码长度必须大于6个字符！');
            } else {
                $User->id = $id;
                $User->password = md5(md5(($password)));
                $User->save();
                $this->success('密码修改成功！', U('Main/main'));
                updateTime();
            }
        }
    }

    public function changeUser()
    {
        sess();
        $cu = M('user')->save(I('post.'));
        if ($cu) {
            $this->success('保存成功', U('Main/main'));
        } else {
            $this->error('保存失败', U('Main/main'));
        }
        updateTime();
    }

    public function onlineUser()
    {
        sess();
        $info = M('user');
        $where['update_time'] = array(
            'gt',
            time() - 600
        );
        $count = $info->where($where)->count(); // 查询满足要求的总记录数 $map表示查询条件
        $Page = getpage($count, 3); // 实例化分页类 传入总记录数
        
        $show = $Page->show(); // 分页显示输出
        
        $list = $info->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    public function bian()
    {
        $data['id'] = session('uid');
        $data['bian'] = I('post.bian');
        $data['update_time'] = time();
        $bian = M('user')->save($data);
        if ($bian) {
            $this->success('保存成功', U('Main/main'));
        } else {
            $this->error('保存失败', U('Main/main'));
        }
    }
}